package com.ctakit.kiwi.common.core.bean.comparators;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lizhenmin
 */
@Slf4j
public abstract class DefaultSortOrderableComparator<T> implements SortOrderableComparator<T> {
    private boolean desc = false;

    @Override
    public int compare(T o1, T o2) {
        int ascResult = ascCompare(o1, o2);
        int finalResult = desc ? 0 - ascResult : ascResult;
        log.debug("desc [{}], ascResult [{}], finalResult [{}]", desc, ascResult, finalResult);
        return finalResult;
    }

    protected abstract int ascCompare(T o1, T o2);

    @Override
    public SortOrderableComparator<T> desc() {
        desc = true;
        return this;
    }

    @Override
    public SortOrderableComparator<T> asc() {
        desc = false;
        return this;
    }
}
